/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package networkrespnse;

import core.BuildInfo;
import core.ParameterDecoder;
import java.util.Vector;
import utils.Utils;
import core.Order;
import core.Settings;

/**
 *
 * @author Kvest
 */
public class PreOrdersResponse extends Response
{
    private Vector orders;

    public PreOrdersResponse(byte[] data)
    {
        super(PREORDERS_RESPONSE);
        orders = new Vector();
        
        parse(data);
    }

    protected void parse(byte[] data)
    {
        OrderResponse orderResp;
        byte[] body,temp;
        int index,size;
        index = 0;
        body = ParameterDecoder.getTransferBody(data);

        temp = new byte[4];
        for (int i=0;i<4;i++)
        {
            temp[i] = body[index++];
        }
        int offset = Utils.byteToInt(temp);
        index += offset;

        String nick = Settings.getSettings().getNick();
        
        //Находим количество заказов
        for (int i=0;i<4;i++)
        {
            temp[i] = body[index++];
        }
        int ordersCount = Utils.byteToInt(temp);

        //Перебираем заказы
        int n;
        String signed, nextSigned;
        for (int ii = 0; ii < ordersCount; ++ii)
        {
            PreOrderAccumulator accumulator = new PreOrderAccumulator();
            
            //Пропускаем размер подпакета
            index += 4;
            
            //Парсим вложенный заказ
            orderResp = createOrderResponse(); 
            orderResp.createOrder();
            
            //Сохраняем текущую позицию курсора
            index = orderResp.parse(body, index);

            //ПОдписавшиеся позывные
            signed = "";
            temp = new byte[4];
            for (int j=0;j<4;j++)
            {
                temp[j] = body[index++];
            }
            n = Utils.byteToInt(temp);

            for (int j = 0; j < n; ++j)
            {
                temp = new byte[4];
                for (int i=0;i<4;i++)
                {
                        temp[i] = body[index++];
                }
                int strSize = Utils.byteToInt(temp);
                temp = new byte[strSize];
                for(int i=0;i<strSize;i++)
                {
                        temp[i] = body[index++];
                }
                nextSigned = Utils.byteToString(temp);
                if (nick.equals(nextSigned)) {
                    accumulator.isDriverSigned = true;
                }
                signed += nextSigned;
                if (j != (n-1))
                {
                    signed += ",";
                }
            }

            accumulator.order = orderResp.getOrder();
            accumulator.signed = signed;
            orders.addElement(accumulator);
       }

        //Подписавшиеся
        for (int ii = 0; ii < ordersCount; ++ii)
        {
            signed = "";
            temp = new byte[4];
            for (int j=0;j<4;j++)
            {
                temp[j] = body[index++];
            }
            n = Utils.byteToInt(temp);

            for (int j = 0; j < n; ++j)
            {
                temp = new byte[4];
                for (int i=0;i<4;i++)
                {
                        temp[i] = body[index++];
                }
                int strSize = Utils.byteToInt(temp);
                temp = new byte[strSize];
                for(int i=0;i<strSize;i++)
                {
                        temp[i] = body[index++];
                }
                nextSigned = Utils.byteToString(temp);
                if (nick.equals(nextSigned)) {
                    ((PreOrderAccumulator)orders.elementAt(ii)).isDriverSigned = true;
                }
                signed += nextSigned;
                if (j != (n-1))
                {
                    signed += ",";
                }
            }

            ((PreOrderAccumulator)orders.elementAt(ii)).addSigned(signed);
        }

        System.gc();
    }

    private OrderResponse createOrderResponse() {
        if (BuildInfo.DISP_ORDER_NUMBER == 4) {
            return new OrderResponse4(Response.ORDER_RESPONSE4);
        } else if (BuildInfo.DISP_ORDER_NUMBER == 3) {
            return new OrderResponse3(Response.ORDER_RESPONSE3);
        } else if (BuildInfo.DISP_ORDER_NUMBER == 2) {
            return new OrderResponse2(Response.ORDER_RESPONSE2);
        } else {
            return new OrderResponse(Response.ORDER_RESPONSE);
        }
    }
    
    public int count()
    {
        return orders.size();
    }

    public PreOrderAccumulator getPreOrder(int index)
    {
        return (PreOrderAccumulator)orders.elementAt(index);
    }

    public class PreOrderAccumulator
    {
        public Order order;
        public String signed = "";
        public boolean isDriverSigned = false;

        public void addSigned(String value)
        {
            if (signed.equals(""))
            {
                signed = value;
            }
            else
            {
                signed += ("," + value);
            }
        }
    }
}
